plugins.AttackAgain = {
	"processSquads" : function() {
		
		setFunc("setSquad", function(e) {
			if (e.value == 'new') {
				$("#addsquad").show();
			} else {
				$("#addsquad").hide();

				if (e.value == 'PresetOff' || e.value == 'PresetDef') {	// send all offensives

					troop_types = {"PresetOff" : ["axe","spy","light","marcher","ram","catapult"],
						       "PresetDef" : ["spear","sword","archer","spy","heavy","knight"]
						      };

					$("a[href*=insertUnit]").each(function() {
						row = $(this).attr("href").replace(/^(.+)\.(.+), ([0-9]+)\)$/, "$2,$3").split(",");
						$("input[name="+row[0]+"]").val('');
						if (troop_types[e.value].indexOf(row[0]) != -1) {
							$("input[name="+row[0]+"]").val(row[1]);
						}
					});

				} else {
					troops = eval(e.value);
				
					for (i in troops) {
						if (troops[i] != 0) {
							$("table.main form input[name="+i+"]").val(troops[i]);
						} else {
							$("table.main form input[name="+i+"]").val('');
						}
					}
				}
			}
		});
		setFunc("removeSquad", function() {
			var sel = $("#squads")[0];
			var oname = sel.options[sel.selectedIndex].text;
			var oval = sel.options[sel.selectedIndex].value;
			
			if (oval != 'new' && oval != 'none') {
				if (window.confirm("Do you really want to remove the '"+oname+"' squad?")) {
					window.setTimeout(function() {
						sel = $("#squads")[0];
						oname = sel.options[sel.selectedIndex].text;
						
						squads = getValue("squads");
						news = {};
						for (i in squads) {
							if (i != oname) {
								news[i] = squads[i];
							}
						}
						setValue("squads", news);
						
						$(sel.options[sel.selectedIndex]).remove();
					}, 0);
					
				}
			}
			
		});
		setFunc("saveSquad", function() {

			if ($("#squadname").val() == '') {
				// error
			} else {
				troops = {};
				empty = true;
				$("table.main form input:text").each(function() {
					if ($(this).attr("name") != 'x' && $(this).attr("name") != 'y') {
						if ($(this).val() == "") { 
							troops[$(this).attr("name")] = 0;
						} else {
							empty = false;
							troops[$(this).attr("name")] = parseInt($(this).val());
						}
					}
				});
				if (! empty) {
				window.setTimeout(function() {
					squads = getValue("squads");
					if (new String(squads) == 'undefined') {
						tmp = {};
						tmp[$("#squadname").val()] = troops;
						setValue("squads", tmp);
					} else {
						squads[$("#squadname").val()] = troops;
						setValue("squads", squads);
					}
					
					document.location.reload();
				}, 0);
				} else {
					alert("There are no troops selected.. what kind of a squad is this?!");
				}
			}
		});
		
		html = '<table><tr><th>Squad</th><th><a href="javascript:removeSquad();" title="Click to remove selected squad"><img src="/graphic/villages.png" /></a></th></tr>'+
				'<tr><td colspan="2"><select id="squads" onchange="setSquad(this);">'+
				'<option value="none" selected="selected">-- select one --</option>';
	
		squads = getValue("squads");
		if (new String(squads) != 'undefined') {
			
			for (i in squads) {
				html += '<option value="'+uneval(squads[i])+'">'+i+'</option>';
			}
		}
		html += '<option value="PresetOff">All offensive</option>';
		html += '<option value="PresetDef">All defensive</option>';
		html += '<option value="new">-- save as new --</option></select></td></tr>'+
				'<tr id="addsquad" style="display:none;"><td><input type="text" id="squadname" /></td><td><a href="javascript:saveSquad();">save</a></td></tr>'+
				'</table>';
		
		if (/mode=command/.test(document.location)) {
			$("table.main form").before(html);
		}
		
	},
	"addLinks" : function() {		
		att_vil_id = $("table.main table.vis a[href*=info_village]:first").attr('href').replace(/(.+)info_village&id=([0-9]+)$/gi, "$2");
		def_vil = $("table.main table.vis a[href*=info_village]:last").html().replace(/(.+) \(([0-9]+)\|([0-9]+)\) K([0-9]+)$/gi, "$2,$3").split(",");
		
		setFunc("attack_again", function(e) {
			jQuery.get(twe.Server+"/game.php?village="+att_vil_id+"&screen=place", '', function(d) {
				
				html  = '<div style="display:block;">';		
				html += $("form", d).parent().html();
				html += '</div>';
				$("table.main table.vis:eq(1) td:eq(2)").append(html);
				
				if (e.value == 'all') {
				$("a[href*=insertUnit]").each(function() { 
					row = $(this).attr("href").replace(/^(.+)\.(.+), ([0-9]+)\)$/, "$2,$3").split(",");
					$("input[name="+row[0]+"]").val(row[1]);
				});
				} else {
					
					troops = eval(e.value);
							
					for (i in troops) {
						$("form input[name="+i+"]").val(troops[i]);
					}
					
				}
				$("input[name=x]").val(def_vil[0]);
				$("input[name=y]").val(def_vil[1]);
				
				$("input[name=attack]").after('<input class="attack" name="attack" value="Attack" type="hidden">').remove();
				
				$("form")[0].submit();
			}, 'html');
		});
		

		html = '<select id="squads" onchange="attack_again(this);">'+
				'<option value="none" selected="selected">-- select one --</option>';
	
		squads = getValue("squads");
		if (new String(squads) != 'undefined') {
			
			for (i in squads) {
				html += '<option value="'+uneval(squads[i])+'">'+i+'</option>';
			}
		}
		html += '<option value="PresetOff">All offensive</option>';
		html += '<option value="PresetDef">All defensive</option>';
		html += '<option value="all">All troops</option></select>';
		
		
		
		$("table.main table.vis:eq(1) td:eq(2)").append('<p>Attack again with '+html+'</p>');
		
	},
	'enhance_game_place' : function() {
		plugins.AttackAgain.processSquads();
	},
	'enhance_game_report' : function() {

		if ($("a[href*=info_village]").length > 0) {
			vils = twe.myVillages();
	
			ids = [];
			for (i in vils) {
				ids.push(vils[i].id);
			}
	
			vilid = $("a[href*=info_village]:first").attr("href").replace(/(.*)&id=(.+)$/,"$2");
	
			if (ids.indexOf(vilid) != -1) {
				plugins.AttackAgain.addLinks();
			}
		}
	}, 
	'info' : {
		'mandatory' : false,
		'name' : "Attack again links in reports",
		'desc' : "Adds links in the reports that will allow for easy sending of troops",
		'version' : "Beta v0.1",
		'dev' : "Timas"
	}
}